package com.zkingsoft.common.actions;

import com.matrix.core.tools.FileType;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.dao.SysCompanyDao;
import com.zkingsoft.common.tools.UploadUtil;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-28 15:14
 */
@Controller
@RequestMapping(value = "/admin/uploadFile")
public class FileUploadAction extends BaseAction {
	/**
	 * 文件夹类型
	 */
	String folderType = "image";
	
	@Autowired
	SysCompanyDao companyDao;

	public final static List<FileType> extList = new ArrayList<FileType>();
	static {
		extList.add(FileType.JPEG);
		extList.add(FileType.PNG);

	}

	@RequestMapping(value = "/doUpload")
	public String doImageUpload(HttpServletResponse response, MultipartHttpServletRequest request)
			throws IOException, FileUploadException, NoSuchAlgorithmException {
		SysUsers user = getSessionUser();
		
		Map<String, String> fileMap = UploadUtil.doUpload(request, extList, folderType, (companyDao.selectById(
																										user.getCompanyId()).
																										getComWebUrl()));
		String callBack = request.getParameter("callBack");
		String inputId = request.getParameter("inputId");
		request.setAttribute("callBack", callBack);
		request.setAttribute("inputId", inputId);
		request.setAttribute("url", fileMap.get("visitPath"));
		request.setAttribute("status", fileMap.get("status"));
		request.setAttribute("msg", fileMap.get("msg"));
		
		return "common/fileUploadResult";
	}
}